/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.service;



import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.db.PurchaseOrder;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.db.Vendor;
import static com.reddys.rad.inventory.service.PurchaseOrderService.em;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import com.reddys.rad.inventory.utils.Utilities;
import java.util.Iterator;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author REDDYS
 */
public class PurchaseOrderService {
    
   static EntityManager em;
    public static ObservableList getProductCategoryList(){
        ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("ProductCategory.findAll");
        List productCategpryList = query.getResultList();
        Iterator iter = productCategpryList.iterator();
        while(iter.hasNext()){
            ProductCategory productCategory = (ProductCategory) iter.next();
            data.add(productCategory.getProductCategoryDescription());
        }
        return data;
    }
    
    public static PurchaseOrderDetails save(PurchaseOrderDetails purchaseOrderDetails,EntityManager em){
        
        if(Utilities.isNull(purchaseOrderDetails.getOrderDetailId())){
            em.persist(purchaseOrderDetails);
        }else{
            em.persist(purchaseOrderDetails);
        }
        Query query = em.createNamedQuery("Product.findByProductId");
        query.setParameter("productId", purchaseOrderDetails.getProductId());
        List productList = query.getResultList();
        if(productList.size()>0){
            Product product = (Product) productList.get(0);
             Query queryProductCategory = em.createNamedQuery("ProductCategory.findByProductCategoryId");
             queryProductCategory.setParameter("productCategoryId", product.getProductCategoryId());
             List productCategoryList = queryProductCategory.getResultList();
             if(productCategoryList.size()>0){
                 ProductCategory productCategory = (ProductCategory) productCategoryList.get(0);
                 product.setProductCategory(productCategory.getProductCategoryDescription());
             }
             purchaseOrderDetails.setProductCategory(product.getProductCategory());
             purchaseOrderDetails.setItemCode(product.getItemCode());
        }
       
        return purchaseOrderDetails;
    }
    
   public static PurchaseOrder saveHeader(PurchaseOrder purchaseOrder,EntityManager em){
       
       if(Utilities.isNull(purchaseOrder.getPurchaseOrderId())){
        em.persist(purchaseOrder);
       }else{
         em.merge(purchaseOrder);  
       }
        return purchaseOrder;
    }

    public static ObservableList getProductList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Product.findAll");
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            Product product = (Product) iter.next();
            data.add(product.getProductId());
        }
        return data;
    }
    
        public static List<Product> getProducts() {
      
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Product.findAll");
        List productList = query.getResultList();
        return productList;
    }
    
    public static Product getProductListById(String productId) {
        em = EntityManagerImpl.getEntityManager();
        Product product = null;
        Query query = em.createNamedQuery("Product.findByProductId");
        query.setParameter("productId", productId);
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            product = (Product) iter.next();
        }
        return product;
    }
        
     public static ObservableList getVendorList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Vendor.findByVendortype");
        query.setParameter("vendortype", "Vendor");
        List vendorList = query.getResultList();
        Iterator iter = vendorList.iterator();
        while(iter.hasNext()){
            Vendor vendor = (Vendor) iter.next();
            data.add(vendor);
            System.out.println("data"+data);
            System.out.println("vendor"+vendor);
            
        }
        return data;
    }
    
}
